package com.lly.pro.service;

import com.lly.pro.common.ResponseJson;
import com.lly.pro.common.ResponseJsonUtil;
import com.lly.pro.common.ResponseStatusCodes;
import com.lly.pro.entity.Admin;
import com.lly.pro.repositories.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author Hommond
 */
@Service
public class AdminService {

    @Autowired
    private AdminRepository adminRepository;

    public Admin findByUserId(long userId){
        return adminRepository.findByUserId(userId);
    }

    //出现异常回滚，不会更改数据库
    @Transactional(rollbackOn = Exception.class)
    public Admin save(Admin admin){
        return adminRepository.save(admin);
    }

    public ResponseJson adminChange(Admin admin){
        return this.adminChange(admin,"修改失败，请检查系统","修改成功");
    }

    public ResponseJson adminChange(Admin admin,String errorMsg ,String successMsg){
        //出现异常代表修改失败，返回99999
        try{
            this.save(admin);
        }catch (Exception e){
            return ResponseJsonUtil.getErrorResult(ResponseStatusCodes.SYSTEM_ERROR,errorMsg);
        }
        return ResponseJsonUtil.getSuccessResult(successMsg);
    }

    public Integer findByUserNameAndPassword(String userName,String password){
        return adminRepository.findByUserNameAndPassword(userName,password);
    }

    public List findUserIdByUserNameAndPassword(String userName,String password){
        return adminRepository.findUserId(userName,password);
    }


    public Long findByUserIdAndPassword(Long userId, String password) {
        return adminRepository.findByUserIdAndPassword(userId,password);
    }
}
